<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-03-08
 * Time: 12:12
 */

namespace app\index\controller;

use app\index\Model\Instock;
use app\index\Model\Instock_item;
use app\index\Model\Stock_pile;
use think\Db;
use think\Request;


class InstockController extends BaseController
{

    public function index(){
        $Instock = new Instock;
        $instock = $Instock->order('create_time','desc')->select();
        if(is_null($instock)){
            return json(['data'=>'errer','msg'=>$this->getError()]);
        }else{
            $this->assign('instock',$instock);
        }
        return $this->fetch();
    }

    public function instockItem(){
        $Item = new Instock_item;
        $item = $Item->order('create_time','desc')->select();
        $this->assign('item',$item);
        return $this->fetch();
    }

    public function Item(){
        $id = Request::instance()->param('id/d');
        $Item = new Instock_item;
        $item = $Item->get($id);
        if (!is_null($item)){
            $instocks = $item->instock()->where(['instock_item_id'=>$id])->select();
        }else{
            $this->error('数据不存在',url('index'));
        }
        $this->assign('instock',$instocks);
        return $this->fetch();
    }

    public function add(){
        $Stockpile_name = new Stock_pile();
        $category = Db::name('Category')->select();
        $max = Instock::max('number')+1;
        if ($max ===1){
            $max = '2017001';
        }
        $stockpile_name = $Stockpile_name->select();
        $this->assign('max',$max);
        $this->assign('category',$category);
        $this->assign('stockname',$stockpile_name);
        return $this->fetch();


    }

    public function addItem()
    {
        $data_item = Request::instance()->param();
        if (Request::instance()->post() === false) {
            return json(['code' => '500', 'data' => 'error', 'msg' => '不支持的请求']);
        } elseif (!$data_item) {
            return json(['code' => '501', 'data' => 'error', 'msg' => '无提交内容']);
        } else {
            $instock_item = new Instock_item();
            $Instock = new Instock;
            $max_item_id = $instock_item->max('id') + 1;
            $new_item = [];
            foreach ($data_item as $key => $value) {
                foreach ($value as $k => $v) {
                    $new_item[$k]['number'] = $data_item['number'][$k];
                    $new_item[$k]['category_id'] = $data_item['category_id'][$k];
                    $new_item[$k]['name'] = $data_item['name'][$k];
                    $new_item[$k]['dimension'] = $data_item['dimension'][$k];
                    $new_item[$k]['spec'] = $data_item['spec'][$k];
                    $new_item[$k]['unit'] = $data_item['unit'][$k];
                    $new_item[$k]['price'] = $data_item['price'][$k];
                    $new_item[$k]['quantity'] = $data_item['quantity'][$k];
                    $new_item[$k]['instock_item_id'] = $max_item_id;
                }
            }
            $data = [
                'pcs' => '',
                'amount' => '',
                'instock_id' => $max_item_id,
            ];
            $validate_instock = $Instock->validate(true)->saveAll($new_item);
            if (false === $validate_instock) {
                return json(['code' => '502', 'data' => 'error', 'msg' => $Instock->getError()]);
            } else {
                $instock_item->save($data);
                $instock = $instock_item->get($max_item_id);
                $pcs = $instock->instock()->where(['instock_item_id' => $max_item_id])->count();
                $amount = $instock->instock()->where(['instock_item_id' => $max_item_id])->sum('price');
                $instock_item->save([
                    'pcs' => $pcs,
                    'amount' => $amount,
                ], ['id' => $max_item_id]);
                $Stockpile = new Stock_pile();//
                $upnumber = '';
                foreach ($new_item as $keys =>$check_item){
                    //判断提交的数据是否为空
                    if(!empty($check_item)){
                        $map = ['category_id'=>$check_item['category_id'],'name'=>$check_item['name'],'spec'=>$check_item['spec'],'unit'=>$check_item['unit']];
           //             $map = ['number'=>$check_item['number']];
     //                   $status = $Stockpile->where($map)->field(['category_id','name','spec','unit'])->select();
                        $status = Stock_pile::get($map);
                        //如果存在批量进行更新
                        if (!is_null($status)){
                            //存在的数据进行更新
                            $quantity = ['quantity'=>$check_item['quantity']];
                            $validate_stockpile = $Stockpile->validate($quantity,'Instock');
                            if(false === $validate_stockpile){
                                return json(['code'=>'502','data'=>'error','msg'=>$Stockpile->getError()]);
                            }
                            $upnumber = Db::name('Stock_pile')->where($map)->setInc('quantity', $check_item['quantity']);
                            //将存在的数据踢出数组
                            unset($new_item[$keys]);
                        }

                    }else{
                        return json(['code'=>'502','data'=>'error','msg'=>$Instock->getError()]);
                    }
                }
                $Stockpile->saveAll($new_item);
                return json(['code'=>'1','data'=>'ok','msg'=>'更新成功：【'.$upnumber.'】条,新增成功：【'.count($new_item).'】条']);
            }
        }
    }

}